package com.contiguous.pair.analyser.filereader;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;

public class FileReader
{
	private Map<File, Exception>	exceptionMap;

	public FileReader()
	{
		exceptionMap = new HashMap<File, Exception>();
	}

	public Map<File, ContiguousPairList> readFiles(File parentPath)
	{
		exceptionMap.clear();
		Map<File, ContiguousPairList> result = new HashMap<File, ContiguousPairList>();
		if (parentPath.isDirectory())
		{
			for (File subPath : parentPath.listFiles())
			{
				if (subPath.isDirectory())
				{
					result.putAll(readFiles(subPath));
				}
				else
				{
					result.putAll(readFile(subPath));
				}
			}
		}
		return result;
	}

	private Map<File, ContiguousPairList> readFile(File subPath)
	{
		Map<File, ContiguousPairList> result = new HashMap<File, ContiguousPairList>();
		try
		{
			String fileAsString = FileUtils.readFileToString(subPath);
			result.put(subPath, new ContiguousPairList(fileAsString));
		}
		catch(IOException e)
		{
			exceptionMap.put(subPath, e);
		}
		return result;

	}
}
